<?php
namespace App\Services\System;

use App\Models\System\LoginLog;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Excel\ExcelExport;
use Illuminate\Support\Facades\DB;

class LoginService
{

    public function getLoginList($request)
    {
        $where = [];
        !empty($request['search']['class_name']) && $where[] = ['students.classroom_name','like','%'.$request['search']['class_name'].'%'];
        !empty($request['search']['user_name']) && $where[] = ['login_logs.user_name','like','%'.$request['search']['user_name'].'%'];
        !empty($request['search']['user_type']) && $where[] = ['login_logs.user_type','like',$request['search']['user_type']];
        if(!empty($request['search']['time'])){
            $time = explode(' - ',$request['search']['time']);
            $where[] = ['login_logs.optime','>=',$time[0]];
            $where[] = ['login_logs.optime','<=',$time[1]];
            //dd(explode(' - ',$request['search']['time']));
        }

        if(!empty($request['terms'])){
            $terms = explode(' - ',$request['terms']);
            $where[] = ['login_logs.optime','>=',$terms[0]];
            $where[] = ['login_logs.optime','<=',$terms[1]];
            //dd(explode(' - ',$request['search']['time']));
        }
        $login = LoginLog::select('login_logs.*','students.classroom_name')
                ->leftJoin('students','login_logs.user_id','=','students.student_id')
                ->where($where)
                ->orderBy('login_logs.optime','desc')
                ->paginate('10')->toArray();
        return $login;
    }

    /**
     * 获取登录详情
     * @author lxy
     * @datetime 2021-04-28
     */
    public function getLoginDetails($request)
    {
        $login = LoginLog::select('login_logs.*','students.classroom_name')
        ->leftJoin('students','login_logs.user_id','=','students.student_id')
        ->where('login_logs.user_id',$request['user_id'])
        ->orderBy('login_logs.optime','desc')
        ->paginate('10')->toArray();
         return $login;
    }

    /**
     * 登录次数统计
     */
    public function getLoginCount($request)
    {
        $where = [];
        !empty($request['search']['class_name']) && $where[] = ['students.classroom_name','like','%'.$request['search']['class_name'].'%'];
        !empty($request['search']['user_name']) && $where[] = ['login_logs.user_name','like','%'.$request['search']['user_name'].'%'];
        !empty($request['search']['user_type']) && $where[] = ['login_logs.user_type','like',$request['search']['user_type']];
        if(!empty($request['search']['time'])){
            $time = explode(' - ',$request['search']['time']);
            $where[] = ['login_logs.optime','>=',$time[0]];
            $where[] = ['login_logs.optime','<=',$time[1]];
            //dd(explode(' - ',$request['search']['time']));
        }
        if(!empty($request['terms'])){
            $terms = explode(' - ',$request['terms']);
            $where[] = ['login_logs.optime','>=',$terms[0]];
            $where[] = ['login_logs.optime','<=',$terms[1]];
            //dd(explode(' - ',$request['search']['time']));
        }
    
        $login = LoginLog::select(DB::raw('count(login_logs.user_id) as user_count,login_logs.*,students.classroom_name'))
                ->leftJoin('students','login_logs.user_id','=','students.student_id')
                ->where($where)
                ->groupBy('login_logs.user_id')
                ->orderBy('login_logs.optime','desc')
                ->paginate('10')->toArray();
        $logincount = LoginLog::where($where)->leftJoin('students','login_logs.user_id','=','students.student_id')->count();
        $login['login_count'] = $logincount;
        return $login;
    }

    public function loginListExport($request)
    {
        $where = [];
        !empty($request['search']['class_name']) && $where[] = ['students.classroom_name','like','%'.$request['search']['class_name'].'%'];
        !empty($request['search']['user_name']) && $where[] = ['login_logs.user_name','like','%'.$request['search']['user_name'].'%'];
        if(!empty($request['search']['time'])){
            $time = explode(' - ',$request['search']['time']);
            $where[] = ['login_logs.optime','>=',$time[0]];
            $where[] = ['login_logs.optime','<=',$time[1]];
            //dd(explode(' - ',$request['search']['time']));
        }
        if(!empty($request['terms'])){
            $terms = explode(' - ',$request['terms']);
            $where[] = ['login_logs.optime','>=',$terms[0]];
            $where[] = ['login_logs.optime','<=',$terms[1]];
            //dd(explode(' - ',$request['search']['time']));
        }
        $reult = LoginLog::select('login_logs.*','students.classroom_name')
                ->leftJoin('students','login_logs.user_id','=','students.student_id')
                ->where($where)
                ->orderBy('login_logs.optime','desc')
                ->get()->toArray();
        $data = [];
        foreach($reult  as $k=>$v){
            $data[$k]['user_name'] = $v['user_name'];
            $data[$k]['classroom_name'] = $v['classroom_name'];
            $data[$k]['optime'] = $v['optime'];
            $data[$k]['ip'] = $v['ip'];
        }

        $field_name = ['姓名','班级','登录时间','ip'];          
        array_unshift($data,$field_name);

        $file_name = '登录记录-'.date('YmdHis').'.xlsx';
        Excel::store(new ExcelExport($data), $file_name, 'excel');
        return [
            'file_name' => $file_name,
            'file_url' => '/download/excel/'.$file_name
        ];
    }

    /**
     * 登录次数导出
     */
    public function loginCountExport($request)
    {
        $where = [];
        !empty($request['search']['class_name']) && $where[] = ['students.classroom_name','like','%'.$request['search']['class_name'].'%'];
        !empty($request['search']['user_name']) && $where[] = ['login_logs.user_name','like','%'.$request['search']['user_name'].'%'];
        if(!empty($request['search']['time'])){
            $time = explode(' - ',$request['search']['time']);
            $where[] = ['login_logs.optime','>=',$time[0]];
            $where[] = ['login_logs.optime','<=',$time[1]];
            //dd(explode(' - ',$request['search']['time']));
        }
        if(!empty($request['terms'])){
            $terms = explode(' - ',$request['terms']);
            $where[] = ['login_logs.optime','>=',$terms[0]];
            $where[] = ['login_logs.optime','<=',$terms[1]];
            //dd(explode(' - ',$request['search']['time']));
        }
        $result = LoginLog::select(DB::raw('count(login_logs.user_id) as user_count,login_logs.*,students.classroom_name'))
                ->leftJoin('students','login_logs.user_id','=','students.student_id')
                ->where($where)
                ->groupBy('login_logs.user_id')
                ->orderBy('login_logs.optime','desc')
                ->get()->toArray();
        $data = [];
        foreach($result  as $k=>$v){
            $data[$k]['user_name'] = $v['user_name'];
            $data[$k]['classroom_name'] = $v['classroom_name'];
            $data[$k]['user_count'] = $v['user_count'];
        }

        $field_name = ['姓名','班级','次数'];          
        array_unshift($data,$field_name);

        $file_name = '登录次数-'.date('YmdHis').'.xlsx';
        Excel::store(new ExcelExport($data), $file_name, 'excel');
        return [
            'file_name' => $file_name,
            'file_url' => '/download/excel/'.$file_name
        ];
    }

    /**
     * 登录详情导出
     */
    public function loginDetailsExport($request)
    {
        $result = LoginLog::select('login_logs.*','students.classroom_name')
        ->leftJoin('students','login_logs.user_id','=','students.student_id')
        ->where('login_logs.user_id',$request['user_id'])
        ->orderBy('login_logs.optime','desc')
        ->get()->toArray();
        $data = [];
        foreach($result  as $k=>$v){
            $data[$k]['user_name'] = $v['user_name'];
            $data[$k]['classroom_name'] = $v['classroom_name'];
            $data[$k]['optime'] = $v['optime'];
            $data[$k]['ip'] = $v['ip'];
        }

        $field_name = ['姓名','班级','登录时间','ip'];          
        array_unshift($data,$field_name);

        $file_name = '登录详情-'.date('YmdHis').'.xlsx';
        Excel::store(new ExcelExport($data), $file_name, 'excel');
        return [
            'file_name' => $file_name,
            'file_url' => '/download/excel/'.$file_name
        ];
    }

}

?>